<template>
  <div class="header">
    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="collapseChage">
      <i v-if="!collapse" class="el-icon-s-fold"></i>
      <i v-else class="el-icon-s-unfold"></i>
    </div>
    <div class="logo">{{ $t("i18n.web_Title") }}</div>
    <div class="header-right">
      <div class="header-user-con">
        <el-button
          size="mini"
          type="danger"
          icon="el-icon-refresh"
          @click="switchLan()"
        >{{ $t("i18n.btn") }}</el-button>
        <!-- 消息中心 -->
        <div class="btn-bell">
          <el-tooltip
            effect="dark"
            :content="message ? `有${message}条未读消息` : `消息中心`"
            placement="bottom"
          >
            <router-link to="/emp">
              <i class="el-icon-bell"></i>
            </router-link>
          </el-tooltip>
          <span class="btn-bell-badge" v-if="message"></span>
        </div>
        <!-- 用户头像 -->
        <div class="user-avator">
          <el-avatar size="medium" :src="photo" @click="changePhoto()"></el-avatar>
        </div>
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ username }}
            <i class="el-icon-caret-bottom"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                icon="el-icon-switch-button"
                divided
                command="loginout"
              >{{ $t("i18n.web_logut") }}</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <div class="el-dialog-content">
      <DialogPhotoComponetVue ref="photo" @loadphoto="loadphoto"></DialogPhotoComponetVue>
    </div>
  </div>
</template>
<script>
import reqVue from "../utils/req.vue";
import DialogPhotoComponetVue from "./DialogPhotoComponet.vue";

export default {
  data() {
    return {
      fullscreen: false,
      name: "",
      message: 0,
      photo: require("../assets/img/img.jpeg")
    };
  },
  components: { DialogPhotoComponetVue },
  computed: {
    username() {
      let username = localStorage.getItem("ms_username");
      return username ? username : this.name;
    },
    collapse() {
      return this.$store.state.collapse;
    }
  },
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command == "loginout") {
        localStorage.removeItem("gloabl_token");
        this.$router.push("/login");
      }
    },
    // 侧边栏折叠
    collapseChage() {
      this.$store.commit("hadndleCollapse", !this.collapse);
    },
    switchLan() {
      var lan = this.$i18n.locale === "zh-cn" ? "en" : "zh-cn";
      localStorage.setItem("key_language", lan);
      this.$i18n.locale = lan;
      reqVue.$Post(null, "emp/v1.0/currentuser").then(
        function(data) {
          this.name =
            lan === "zh-cn"
              ? data.body.empName
              : data.body.empEnName == null || data.body.empEnName == ""
              ? data.body.empName
              : data.body.empEnName;
          window.location.href = "/";
        }.bind(this)
      );
    },
    loadPhoto() {
      reqVue.$Post(null, "file/v1.0/photo/0", "doc").then(
        function(data) {
          if (data.status) {
            if (data.body !== "") this.photo = data.body;
          } else {
            this.$message.error(data.message);
          }
        }.bind(this)
      );
    },
    changePhoto() {
      this.$refs.photo.cropImg = this.photo;
      this.$refs.photo.dialogPhotoVisible = true;
    },
    loadphoto(photo) {
      if (photo !== "") this.photo = photo;
    },
    initData(photo, employee) {
      this.name =
        this.$i18n.locale === "zh-cn"
          ? employee.empName
          : employee.empEnName == null || employee.empEnName == ""
          ? employee.empName
          : employee.empEnName;
      if (photo !== "") this.photo = photo;
    }
  },
  mounted() {
    this.$i18n.locale = localStorage.getItem("key_language");
    if (document.body.clientWidth < 1500) {
      this.collapseChage();
    }
  }
};
</script>
<style scoped>
.header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  font-size: 22px;
  color: #fff;
}
.collapse-btn {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 50px;
}
.header .logo {
  float: left;
  width: 450px;
  line-height: 50px;
}
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  height: 50px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}
.btn-bell .el-icon-bell {
  color: #fff;
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  margin: 0 3px;
  margin-top: 7px;
}
.user-avator_img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
</style>
